//
//  TabBarViewController.swift
//  CloudMachineLeasing
//
//  Created by xielihua on 2018/10/30.
//  Copyright © 2018年 polarLee. All rights reserved.
//

import UIKit
//import IQKeyboardManagerSwift
class TabBarViewController: UITabBarController,UITabBarControllerDelegate {
    
    //动画数据源
    var imagesArray : Array = [Any]()

    override func viewDidLoad() {
        super.viewDidLoad()
        initView()
        initImagesArray()
    }
    func initView() {
        self.setControllers()
        addBind()//绑定数据
//        NotificationCenter.default.addObserver(self, selector: #selector(handleApp(_:)), name: NSNotification.Name(rawValue: "LianXinHandle"), object: nil)
        self.delegate = self
//        let manger = IQKeyboardManager.shared
//        manger.enable = true
//        manger.enableAutoToolbar = false
//        manger.shouldResignOnTouchOutside = true
    }
    func initImagesArray() {
//        for i in 0...2 {
//            var images: Array = [UIImage]()
//            switch i {
//            case 0:
//                for j in 0...11 {
//                    let imageName = "首页_000\(j)"
//                    if let tabcgImage = UIImage.init(named: imageName) {
//                        images.append(tabcgImage)
//                    }
//                }
//            case 1:
//                for j in 0...11 {
//                    let imageName = "项目_000\(j)"
//                    if let tabcgImage = UIImage.init(named: imageName) {
//                        images.append(tabcgImage)
//                    }
//                }
//            case 2:
//                for j in 0...11 {
//                    let imageName = "我的_000\(j)"
//                    if let tabcgImage = UIImage.init(named: imageName) {
//                        images.append(tabcgImage)
//                    }
//                }
//            default:
//                break
//            }
//            imagesArray.append(images)
//        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    //MARK: 绑定数据
    func addBind() {
//        LoginViewModel.sharedInstance.companySign.output.observeResult { (result) in
//            if let str = try? result.get() {
//                if str == ChangeCompanyType.selectCompany.rawValue {
//                    if LoginViewModel.sharedInstance.selectCompanyModel?.companyID == nil {
//                        if self.selectedIndex < (self.viewControllers?.count ?? 0), let visibleController = self.viewControllers?[self.selectedIndex] as? UINavigationController {
//                            let selectCompanyController = ChangeCompanyViewController()
//                            selectCompanyController.hidesBottomBarWhenPushed = true
//                            visibleController.pushViewController(selectCompanyController, animated: true)
//                        }
//
//                    }
//                }
//            }
//        }
    }
    private func setControllers() {
//        let controllers = [MainPageNewViewController(), ProjectHomePageViewController(), NewMeViewController()]
//        let titleArray = ["首页", "项目", "我的"]
//        //        let normalImage = ["tab_home", "tab_item", "tab_porfile"]
//        let normalImage = ["首页_0000", "项目_0000", "我的_0000"]
//        //        let selectedImage = ["tab_home_pre", "tab_item_pre", "tab_porfile_pre"]
//        let selectedImage = ["首页_00011", "项目_00011", "我的_00011"]
//        for index in 0...controllers.count - 1 {
//            self.addChildVC(controllers[index],titleArray[index],normalImage[index],
//            selectedImage[index])
//        }
    }
    //链信拉起云租app 申请企业
    @objc func handleApp(_ notification: NSNotification) {
//        let appDelegate = UIApplication.shared.delegate as? AppDelegate
//        DealWithHandleApp().dealWithHandle(appDelegate?.iphone, appDelegate?.userName, appDelegate?.companyName)
//    }
//    private func addChildVC(_ childVC:UIViewController, _ barItemTitle:String, _ normalImage:String, _ selectedImage:String) {
//        let nav = NavigationController.init(rootViewController: childVC)
//        nav.title = barItemTitle
//        childVC.tabBarItem.title = barItemTitle
//        childVC.tabBarItem.image = UIImage(named: normalImage)?.withRenderingMode(.alwaysOriginal)
//        childVC.tabBarItem.selectedImage = UIImage(named: selectedImage)?.withRenderingMode(.alwaysOriginal)
//        self.addChild(nav)
    }
    
     func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
//        if viewController.title == "项目" {
//            if !LoginViewModel.sharedInstance.isLogin {
//                self.view.makeTopToast("登录后才能使用项目！")
//                return false
//            } else if LoginViewModel.sharedInstance.selectCompanyModel?.companyID == nil {
//                if let visibleController = self.viewControllers?[self.selectedIndex] as? UINavigationController {
//                    let controller = ChangeCompanyViewController()
//                    controller.closeBlock = {[weak self] in
//                        controller.navigationController?.popViewController(animated: false)
//                        self?.selectedIndex = 1
//                    }
//                    controller.hidesBottomBarWhenPushed = true
//                    visibleController.pushViewController(controller, animated: true)
//                }
//                return false
//            }
//        } else if viewController.title == "我的" {
////            if LoginViewModel.sharedInstance.isLogin, let _ =  LoginViewModel.sharedInstance.inforModel {
////                LoginViewModel.sharedInstance.loginSign.input
////                    .send(value:LoginType.changeTabToMe.rawValue)
////            }
//        }
        return true
    }

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
//        var tabBarBtnArray : Array = [UIView]()
//        let index : Int! = tabBar.items?.firstIndex(of: item)
//        //获取UITabBarButton
//        for i in  0..<tabBar.subviews.count {
//            let tabBarButton = tabBar.subviews[i]
//            if let btnClass = NSClassFromString("UITabBarButton") {
//                if tabBarButton.isKind(of: btnClass) {
//                    tabBarBtnArray.append(tabBarButton)
//                }
//            }
//        }
//        if tabBarBtnArray.count > 0 {
//            //获取当前的UITabBarButton
//            let tabBarButton = tabBarBtnArray[index]
//            for i in 0..<tabBarBtnArray.count {
//                if let imageView = getTabBarImageView(view: tabBarBtnArray[i]) {
//                    imageView.stopAnimating()
//                    if i != index {
//                        imageView.animationImages = getDefaultImages(index: i)
//                    }
//                }
//            }
//            let images : Array = imagesArray[index] as! Array<UIImage>
//            if let imageView = getTabBarImageView(view: tabBarButton) {
//                imageView.animationImages = images
//                imageView.animationDuration = Double(images.count) * 0.04167
//                imageView.animationRepeatCount = 1
//                imageView.startAnimating()
//            }
//        }
    }
    ///获取TabBarButton中的图片控件
//    func getTabBarImageView(view: UIView) -> UIImageView? {
////        for imageV in view.subviews {
////            if let ivClass = NSClassFromString("UITabBarSwappableImageView") {
////                if imageV.isKind(of: ivClass) {
////                    let imageView : UIImageView = imageV as! UIImageView
////                    return imageView
////                }
////            }
////        }
////        return nil
//    }
    func getDefaultImages(index: Int) -> [UIImage] {
        switch index {
        case 0:
            return [UIImage.init(named: "首页_0000")!]
        case 1:
            return [UIImage.init(named: "项目_0000")!]
        case 2:
            return [UIImage.init(named: "我的_0000")!]
        default:
            return []
        }
    }
}
